<?php
/**
 * appRain v 0.1.x
 *
 * LICENSE
 *
 * This source file is subject to the MIT license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.opensource.org/licenses/mit-license.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@apprain.com so we can send you a copy immediately.
 *
 * @copyright  Copyright (c) 2010 appRain, Team. (http://www.apprain.com)
 * @license    http://www.opensource.org/licenses/mit-license.php MIT license
 *
 * HELP
 *
 * Official Website
 * http://www.apprain.com/
 *
 * Download Link
 * http://www.apprain.com/download
 *
 * Documents Link
 * http ://www.apprain.com/docs
 */
 ?>

<?php if( $action == 'update' ||  $action == 'add' ): ?>

<?php
    $adminlist = isset($this->data["Admin"]) ? $this->data["Admin"] : $adminlist;
    $adminlist['id'] = isset( $adminlist['id'] ) ? $adminlist['id'] : "";
    $adminlist['f_name'] = isset( $adminlist['f_name'] ) ? $adminlist['f_name'] : "";
    $adminlist['l_name'] = isset( $adminlist['l_name'] ) ? $adminlist['l_name'] : "";
    $adminlist['username'] = isset( $adminlist['username'] ) ? $adminlist['username'] : "";
    $adminlist['password'] = isset( $adminlist['password'] ) ? $adminlist['password'] : "";
    $adminlist['cpassword'] = isset( $adminlist['cpassword'] ) ? $adminlist['cpassword'] : $adminlist['password'];
    $adminlist['email'] = isset( $adminlist['email'] ) ? $adminlist['email'] : "";
    $adminlist['acl'] = isset( $adminlist['acl'] ) ? $adminlist['acl'] : "";
    $adminlist['description'] = isset( $adminlist['description'] ) ? $adminlist['description'] : "";
    $adminlist['status'] = isset( $adminlist['status'] ) ? $adminlist['status'] : "";
    $adminlist['type'] = isset( $adminlist['type'] ) ? $adminlist['type'] : "Normal";

    /*
     * Set data validation fields
     */
    $validation_map = array(
                             "f_name" => "NULL",
                             "l_name" => "NULL"
                            );

    if(  $action == 'add'  )
    {
        $validation_map['username'] = 'NULL';
        $validation_map['email']    = 'EMAIL';
        $validation_map['password|cpassword'] = 'MATCH';
    }

    /*
     * Calling validation
     */
    echo App::Load("Helper/Javascript")->validation_map(array('_errBg'=>ERROR_BACKGROUND,'_dflBg'=>"#CECECE",'_autoSubmit'=>'true','_errToolTips'=>'false','_errorMark'=>'inline'));

    /*
     * Call POST form
     */
    echo $this->get_tag('form',array('action'=>$this->baseurl("/admin/manage/$action/" . $adminlist['id']),'method'=>'post','enctype'=>'multipart/form-data','class'=>'app_form app_validation','id'=>'auto_chk_submit'));

        /*
         * Call the tools bar
         */
        echo $this->callElement("default/tools/tool_bar",array("buttons"=>array(array("type"=>"save"),array("type"=>"file_manager"),array("type"=>"back")) ,"title" => $action . " Admin "));
        /*
         * Generate submit form
         */
        $Grid = App::Module('DataGrid')->setDisplay('FormListing')
                                       ->addRow("* First Name",App::load("Helper/Html")->inputTag("data[Admin][f_name]",$adminlist['f_name'],array("id"=>"f_name","class"=>"app_input check_notempty","longdesc"=>"Please enter  first name")))
                                       ->addRow("* Last Name",App::load("Helper/Html")->inputTag("data[Admin][l_name]",$adminlist['l_name'],array("id"=>"l_name","class"=>"app_input check_notempty","longdesc"=>"Please enter last name")))
                                       ->addRow("* Email",App::load("Helper/Html")->inputTag("data[Admin][email]",$adminlist['email'],array("id"=>"email","class"=>"app_input check_email","longdesc"=>"Please enter email address")));

        if($action == 'add')
        {
            $Grid->addRow("* Username",App::load("Helper/Html")->inputTag("data[Admin][username]",$adminlist['username'],array("id"=>"username","class"=>"app_input check_notempty","longdesc"=>"Please enter a valid user name")))
                 ->addRow("* Password",App::load("Helper/Html")->passwordTag("data[Admin][password]",$adminlist['password'],array("id"=>"password","class"=>"app_input check_notempty","longdesc"=>"Please enter with combination of A-Za-z0-9")));
        }

        $Grid->addRow("Description",App::load("Helper/Html")->textareaTag("data[Admin][description]",$adminlist['description'],array("id"=>"description","class"=>"app_input")));

        if( strtolower($adminlist['type']) != 'supper')
        {
            $Grid->addRow("* Status",App::load("Helper/Html")->selectTag("data[Admin][status]",array("Active"=>"Active","Pending"=>"Pending"),$adminlist['status']));
        }
        ?>

        <table width="100%" border="0">
        <tr>
            <td width="60%" valign="top" style="padding-top:6px">
                <?php echo $this->callElement('default/tools/message', array('caption' =>'Message','type' => 'error', 'message' => $error)); ?>
                <?php $Grid->Render();?>
            </td>
            <?php if( strtolower($adminlist['type']) != 'supper'):?>
            <td valign="top">
                <div class="app_box">
                    <h3 class="app_box_title"><span class="box-button">Access Control Limit</span></h3>
                    <div class="box-body">
                        <div style="padding:10px">
                            <?php echo App::Load("Helper/Html")->checkboxTag("data[Admin][acl][]",$admin_nav_def,$adminlist['acl'],null,array('html_wrapper'=>'br'));?>
                        </div>
                    </div>
                </div>
            </td>
            <?php endif;?>
        </tr>
        </table>

        <?php
        echo $this->callElement("default/tools/tool_bar",array("buttons"=>array( array("type"=>"save"), array("type"=>"back")) ,"position" => 'bottom'));
        echo App::load("Helper/Html")->hiddenTag("data[Admin][id]",$adminlist['id']);
    echo $this->get_tag("/form");
?>
<?php elseif ( $action =='view'): ?>
    <div class="app_form">
        <?php echo $this->callElement("default/tools/tool_bar",array("buttons"=>array( array("type" => "back")),"title" => "{$adminlist['f_name']} {$adminlist['l_name']}"));?>
        <table width="100%" border="0">
        <tr>
            <td width="60%" valign="top" style="padding-top:6px">
                <?php
                    App::Module('DataGrid')->setDisplay('FormListing')
                                           ->addRow("First Name",$adminlist['f_name'])
                                           ->addRow("Last Name",$adminlist['l_name'])
                                           ->addRow("Email",$adminlist['email'])
                                           ->addRow("User Name",$adminlist['username'])
                                           ->addRow("Date Signup",App::Load("Helper/Date")->dateFormated($adminlist['createdate']))
                                           ->addRow("Date Signup",App::Load("Helper/Date")->dateFormated($adminlist['lastlogin']))
                                           ->addRow("Description",$adminlist['description'])
                                           ->addRow("Status",$adminlist['status'])
                                           ->Render();
                ?>
            </td>
            <?php if( strtolower($adminlist['type']) != 'supper'):?>
            <td valign="top">
                <div class="app_box">
                    <h3 class="app_box_title"><span class="box-button">Access Control Limit</span></h3>
                    <div class="box-body">
                        <div style="padding:10px">
                            <?php echo App::Load("Helper/Html")->checkboxTag("data[Admin][acl][]",$admin_nav_def,$adminlist['acl'],null,array('html_wrapper'=>'br'));?>
                        </div>
                    </div>
                </div>
            </td>
            <?php endif; ?>
        </tr>
        </table>
        <?php  echo $this->callElement("default/tools/tool_bar",array("buttons"=>array( array("type" => "back")),"position" => 'bottom')); ?>
    </div>
    <br class="clearboth" />

<?php else: ?>

    <?php  echo $this->callElement("default/tools/tool_bar",array("buttons"=>array( array('type'=>'back'),array('type'=>'delete')) ,"title" => "View Administrators"));?>

    <?php if(!empty($adminlist["data"])): ?>
        <?php

           #function addButtons($sender, $e)
           # {
           #   $e->cells[5] = ($e->cells[5]=='Supper') ? App::Helper('Html')->getTag('strong',Array('class'=>'supper-admin-name'),'Super') : $e->cells[5];
           # }

            $Grid = App::Module('DataGrid');
            $Grid->setHeader(array($this->get_tag('input',array('type'=>'checkbox','id'=>'checkall','class'=>"{'mode':'Admin'}")),'#','Login Name', 'Name', 'Email','Status','Type','Options'));
            $Grid->setFooter($adminlist['link'] . " " . $adminlist['paging'] . " &nbsp;&nbsp; {$this->__('Total Entry')} : " . $adminlist["total"]);
            foreach($adminlist["data"] as $key => $val):       
                            $ctrlLinks =(strtolower($val['type'])=='supper') ?
                            App::load("Helper/Html")->linkTag($this->baseurl('/admin/manage/view/' . $val['id'] ),App::load("Helper/Html")->imgTag($this->baseurl('/images/admin/view.gif'),array('title' => 'View ',NULL,'alt'=>'View',"class"=>"icon")),array("title"=>"View user information"))
                            . App::load("Helper/Html")->linkTag($this->baseurl('/admin/manage/update/' . $val['id']),App::load("Helper/Html")->imgTag($this->baseurl('/images/admin/edit.gif'),NULL,array('title'=>'Edit ','alt' => 'Edit',"class"=>"icon")),array("title"=>"Edit user information"))
                            :
                            App::load("Helper/Html")->linkTag($this->baseurl('/admin/manage/view/' . $val['id'] ),App::load("Helper/Html")->imgTag($this->baseurl('/images/admin/view.gif'),array('title' => 'View ',NULL,'alt'=>'View',"class"=>"icon")),array("title"=>"View user information"))
                            . App::load("Helper/Html")->linkTag($this->baseurl('/admin/manage/update/' . $val['id']),App::load("Helper/Html")->imgTag($this->baseurl('/images/admin/edit.gif'),NULL,array('title'=>'Edit ','alt' => 'Edit',"class"=>"icon")),array("title"=>"Edit user information"))
                            . App::load("Helper/Html")->linkTag("javascript:void(0)",App::load("Helper/Html")->imgTag($this->baseurl('/images/admin/remove.gif'),NULL,array('title'=>'Delete','alt'=>'Delete')),array('id'=>'Admin','class'=>'link_delete icon', 'title'=>'Admin_' . $val['id']));
                $Grid->addRow($this->get_tag('input',array('type'=>'checkbox','value'=>"{$val['id']}","class"=>"checkrow")),(($adminlist["page"] - 1) * $this->get_config('default_pagination') + $key + 1),$val['username'],"{$val['f_name']} {$val['l_name']}", $val['email'], $val['status'], $val['type'], $ctrlLinks);
            endforeach;
           // $Grid->ondatabind->Subscribe("addButtons");
            $Grid->Render();
        ?>

    <?php else: ?>
        <?php echo $this->callElement('default/tools/message', array('caption' =>'Message','type' => 'error', 'message' => 'No Data found')); ?>
    <?php endif; ?>

    <?php echo $this->callElement("default/tools/tool_bar",array("buttons"=>array(array('type'=>'back'),array('type'=>'delete')) ,"position" => 'bottom')); ?>

<?php endif; ?>